package com.aifund.app.controller;

import com.aifund.app.entity.form.tactices.FollowExchangeManTacticsForm;
import com.aifund.app.entity.form.tactices.FollowRobotForm;
import com.aifund.app.entity.form.tactices.GetExchangeMansForm;
import com.aifund.app.entity.form.tactices.GetRobotForm;
import com.aifund.app.entity.vo.robot.GetExchangeManTacticsVo;
import com.aifund.app.entity.vo.robot.GetRobotsVo;
import com.aifund.app.service.FollowOrderManTacticsService;
import com.aifund.app.service.RobotService;
import com.aifund.app.service.UserFollowRobotService;
import com.aifund.app.service.UserService;
import com.aifund.common.entity.AiRobot;
import com.aifund.common.result.ResultBody;
import com.aifund.common.util.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 策略
 */
@RestController
@RequestMapping("/api/tactics")
@Api(tags = "策略")
public class TacticsController {

    @Autowired
    RobotService robotService;
    @Autowired
    UserService userService;
    @Autowired
    UserFollowRobotService userFollowRobotService;
    @Autowired
    FollowOrderManTacticsService followOrderManTacticsService;
    @Autowired
    JwtUtil jwtUtil;


    /**
     * 获取AI智能机器人
     * @param form
     * @return
     */
    @PostMapping("/getRobots")
    @ApiOperation("获取AI智能机器人")
    public ResultBody<List<GetRobotsVo>> getRobots(@RequestBody GetRobotForm form){
        List<AiRobot> aiRobots = robotService.getAll(form.getExchangeId());
        List<GetRobotsVo> getRobotsVos = new ArrayList<>();
        for(AiRobot aiRobot : aiRobots){
            GetRobotsVo getRobotsVo = new GetRobotsVo();
            BeanUtils.copyProperties(aiRobot,getRobotsVo);
            //用户数量
            getRobotsVo.setUserNumber(userFollowRobotService.getCountForRobot(aiRobot.getId()));
            getRobotsVos.add(getRobotsVo);
        }
        return ResultBody.success(getRobotsVos);
    }

    /**
     * 获取交易策略
     * @param form
     * @return
     */
    @PostMapping("/getExchangeTactics")
    @ApiOperation("获取交易策略")
    public ResultBody<List<GetExchangeManTacticsVo>> getExchangeTactics(@RequestBody GetExchangeMansForm form){
        List<GetExchangeManTacticsVo> exchangeMans = userService.getExchangeTactics(form.getExchangeId());
        return ResultBody.success(exchangeMans);
    }


    @PostMapping("/followExchangeManTactics")
    @ApiOperation("跟单交易员策略")
    public ResultBody followExchangeManTactics(@RequestBody @Validated FollowExchangeManTacticsForm form, HttpServletRequest request){
        followOrderManTacticsService.followExchangeManTactics(form,jwtUtil.getUserId(request));
        return ResultBody.success();
    }

    @PostMapping("/followRobot")
    @ApiOperation("跟单机器人")
    public ResultBody followRobot(@RequestBody @Validated FollowRobotForm form, HttpServletRequest request){
        followOrderManTacticsService.followRobot(form,jwtUtil.getUserId(request));
        return ResultBody.success();
    }


}
